package com.yanqu.road.dao.impl.activity.jiuzhou.config;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.jiuzhou.config.JiuZhouRoundConfig;
import com.yanqu.road.entity.activity.jiuzhou.config.JiuZhouSkillConfig;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class JiuZhouConfigDaoImpl extends TempDao {

    private static String getJiuZhouRoundConfigSql = "select * from `t_s_activity_ninechess_round` where `activityId`=?";

    public Map<Integer, JiuZhouRoundConfig> getJiuZhouRoundConfigMap(int activityId) {
        Map<Integer, JiuZhouRoundConfig> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getJiuZhouRoundConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                JiuZhouRoundConfig config = new JiuZhouRoundConfig();
                config.setActivityId(rs.getInt("activityId"));
                config.setId(rs.getInt("id"));
                config.setType(rs.getInt("type"));
                String upRank = rs.getString("upRank");
                if (!StringUtils.isNullOrEmpty(upRank)) {
                    config.setUpRank(StringUtils.stringToIntegerList(upRank, "\\|"));
                }
                config.setScore(rs.getString("score"));
                config.setTime(rs.getString("time"));
                config.setMatchRule(rs.getString("matchRule"));
                config.setReward(rs.getString("reward"));
                config.setFightParam(rs.getString("fightParam"));
                config.setGuessParam(rs.getString("guessParam"));
                map.put(config.getId(), config);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    private static String getJiuZhouSkillConfigSql = "select * from `t_s_activity_ninechess_skill` where `activityId`=?";

    public Map<Integer, JiuZhouSkillConfig> getJiuZhouSkillConfigMap(int activityId) {
        Map<Integer, JiuZhouSkillConfig> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getJiuZhouSkillConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                JiuZhouSkillConfig config = new JiuZhouSkillConfig();
                config.setActivityId(rs.getInt("activityId"));
                config.setId(rs.getInt("id"));
                config.setName(rs.getString("name"));
                config.setDesc(rs.getString("desc"));
                config.setType(rs.getInt("type"));
                config.setSkillParam(rs.getString("skillParam"));
                config.setWeight(rs.getInt("weight"));
                config.setItemID(rs.getInt("itemID"));
                config.setTag(rs.getInt("tag"));
                config.setUseNum(rs.getInt("useNum"));
                map.put(config.getId(), config);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

}
